<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" placeholder="请输入1-100之间的整数" name="" id="" />
    <input type="button" value="猜一猜" onclick="f()" />
    <h1>结果：<span></span></h1>

    <script>
      let x = parseInt(Math.random() * 100) + 1;
      let num = document.querySelector("input");
      let s = document.querySelector("span");
      let flag = 0;
      function f() {
        flag++;
        if (num.value > x) {
          s.innerText = "猜大了";
        }
        if (num.value < x) {
          s.innerText = "猜小了";
        }
        if (num.value == x) {
          s.innerText = "猜对了,用了" + flag + "次";
        }
      }
    </script>
  </body>
</html>
